// DOM，事件类型，事件处理函数
function addEvent(el, type, fn) {
  if (el.addEventListener) {
    el.addEventListener(type, fn, false);
  } else if (el.attachEvent) {
    el.attachEvent('on' + type, function() {
      // 改变this指向，指向el
      fn.call(el);
    });
  } else {
    el['on' + type] = fn;
  }
}

function removeEvent(elem, type, fn) {
  if (elem.addEventListener) {
    elem.removeEventListener(type, fn, false);
  } else if (elem.attachEvent) {
    el.detachEvent('on' + type, fn);
  } else {
    elem['on' + type] = null;
  }
}

// 解除事件处理函数
// el.removeEventListener(type, fn, false);
// el.detachEvent('on' + type, fn)
// el['on' + type] = null;

// 获取CSS属性
function getStyles(elem, attr) {
  if (window.getComputedStyle) {
    if (attr) {
      return parseInt(window.getComputedStyle(elem, null)[attr]);
    } else {
      return window.getComputedStyle(elem, null);
    }
  } else {
    if (prop) {
      return parseInt(elem.currentStyle[prop]);
    } else {
      return elem.currentStyle;
    }
  }
}

// 获取元素宽高
function getScrollSize() {
  if (document.body.scrollWidth && document.body.scrollHeight) {
    return {
      width: document.body.scrollWidth,
      height: document.body.scrollHeight,
    };
  } else {
    return {
      width: document.documentElement.scrollWidth,
      height: document.documentElement.scrollHeight,
    };
  }
}

function pagePos(e) {
  var sLeft = getScrollOffset().left,
    sTop = getScrollOffset().top,
    cLeft = document.documentElement.clientLeft || 0,
    cTop = document.documentElement.clientTop || 0;
  return {
    X: e.clientX + cLeft - cLeft,
    Y: e.clientY + sTop - cTop,
  };
}

// 获取可视窗口宽高
function getViewportSize() {
  if (window.innerWidth) {
    return {
      width: window.innerWidth,
      height: window.innerHeight,
    };
  } else {
    if (document.compatMode === 'CSS1Compat') {
      return {
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
      };
    } else {
      return {
        width: document.body.clientWidth,
        height: document.body.clientHeight,
      };
    }
  }
}

// 获取滚动条距离
function getScrollOffset() {
  if (window.scrollX) {
    return {
      top: window.scrollY,
      left: window.scrollX,
    };
  } else {
    return {
      top: document.body.scrollTop + document.documentElement.scrollTop,
      left: document.body.scrollLeft + document.documentElement.scrollLeft,
    };
  }
}

// childNode 转换为 children
function elemChildren(node) {
  var temp = {
      'length': 0,
      'splice': Array.prototype.splice,
    },
    children = node.childNodes;
  for (var i = 0; i < children.length; i++) {
    var childItem = children[i];
    if (childItem.nodeType === 1) {
      temp[temp['length']] = childItem;
      temp['length']++;
    }
  }
  return temp;
}

// 禁止冒泡
function cancelBubble(e) {
  var e = e || window.event;
  if (e.stopPropagation) {
    e.stopPropagation();
  } else {
    e.cancelBubble = true;
  }
}

function preventDefaultEvent(e) {
  var e = e || window.event;
  if (e.preventDefault) {
    e.preventDefault();
  } else {
    e.returnValue = false;
  }
}

// 获取元素的上层元素
function elemParent(node, n) {
  var buffer = node;
  for (var i = 0; i < n; i++) {
    buffer = buffer.parentNode;
    if (buffer === null) {
      return undefined;
    }
  }
  return buffer;
}

/*
*  判断点是否在一个三角形内（鼠标预测），a、右上角、右下角位置构成三角形，
* 当前鼠标的位置在这个范围内则返回true，不在返回false
*   参数1：p点的值（当前鼠标位置）
*   参数2：a点的值（当前鼠标上一个位置）
*   参数3：右上角位置
*   参数4：右下角位置
* */
var pointInTriangle = (function() {
  function vec(a, b) {
    return {
      x: b.x - a.x,
      y: b.y - a.y,
    };
  }
  
  function vecProduct(v1, v2) {
    return v1.x * v2.y - v2.x * v1.y;
  }
  
  function sameSymbols(a, b) {
    return (a ^ b) >= 0;
  }
  
  return function(opt) {
    var PA = vec(opt.curPos, opt.lastPos),
      PB = vec(opt.curPos, opt.topLeft),
      PC = vec(opt.curPos, opt.bottomLeft),
      R1 = vecProduct(PA, PB),
      R2 = vecProduct(PB, PC),
      R3 = vecProduct(PC, PA);
    
    return sameSymbols(R1, R2) && sameSymbols(R2, R3);
  };
})();
